package com.yuyou.fn.settlement.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.settlement.entity.IncomeCategory;
import com.yuyou.fn.settlement.mapper.IncomeCategoryMapper;
import com.yuyou.fn.settlement.service.IIncomeCategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * IncomeCategory 表数据服务层接口实现类
 *
 */
@Service
public class IncomeCategoryServiceImpl extends SuperServiceImpl<IncomeCategoryMapper, IncomeCategory,Long> implements IIncomeCategoryService {


    @Override
    public Page<IncomeCategory> findIncomeCategory(int pageNo, int pageSize) {
        Example<IncomeCategory> example = IncomeCategory.createExample();
        example.orderBy("createTime");
        return findPageByExample(example,pageNo,pageSize );
    }

    @Override
    public List<IncomeCategory> findAllIncomeCategory() {
        return findListByExample(IncomeCategory.createExample());
    }

    @Override
    public void saveOrUpdateIncomeCategory(Long incomeCategoryId, String name, String remark) {

        IncomeCategory incomeCategory = new IncomeCategory();
        incomeCategory.setName(name);
        incomeCategory.setRemark(remark);
        incomeCategory.setUpdateTime(TimeUtils.now());
        if(incomeCategoryId == null ){
            incomeCategory.setCreateTime(TimeUtils.now());
            incomeCategory.setIsBuildIn(0);
            insertSelective(incomeCategory);
        }else{
            incomeCategory.setIncomeCategoryId(incomeCategoryId);
            updateSelectiveById(incomeCategory);
        }

    }

    @Override
    public void deleteIncomeCategory(Long incomeCategoryId) {

        IncomeCategory incomeCategory = findById(incomeCategoryId);

        if(incomeCategory != null ){

            if(incomeCategory.getIsBuildIn() == 1 ){
                throw new BusException("内置类型不能删除") ;
            }

            deleteById(incomeCategoryId);

        }
    }

}